"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : MOT
Case Name   : 结合MOT表，使用join命令操作表(内联、左联、右联、全外联)
Create At   : 2020/09/18
Owner       : opentestcase028
Description :
    1.设置enable_incremental_checkpoint参数为off,并重启数据库
    2.创建普通MOT表,插入数据,join命令操作,清理数据
    3.恢复环境默认配置，修改enable_incremental_checkpoint为on，并重启数据库
Expect      :
    1.设置成功，重启数据库成功
    2.操作成功，清理数据成功
    3.修改参数成功，重启数据库成功
History     :
    modified by opentestcase010 at 2023/2/6，优化MOT用例
"""

import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class Mot_datatype_test(unittest.TestCase):

    def setUp(self):
        self.logger = Logger()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.commonsh = CommonSH
        self.common = Common()
        self.config = 'enable_incremental_checkpoint'
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.logger.info('-----修改配置，并重启数据库-----')
        show_msg = self.common.show_param(self.config)
        if 'off' not in show_msg:
            msg1 = self.primary_sh.execute_gsguc('set',
                                             self.constant.GSGUC_SUCCESS_MSG,
                                             f'{self.config}=off')
            self.logger.info(msg1)
            msg2 = self.primary_sh.restart_db_cluster()
            self.logger.info(msg2)
            msg3 = self.primary_sh.get_db_cluster_status()
            self.assertTrue("Normal" or "Degrade" in msg3)
            show_msg = self.common.show_param(self.config)
            self.assertTrue('off' in show_msg)

    def test_MOT_Vacuum(self):
        text = '-----step:创建普通MOT表,插入数据,join命令操作,清理数据;' \
               'expect:成功-----'
        self.logger.info(text)
        self.t_name_1 = 't_mot_0101_1'
        self.t_name_2 = 't_mot_0101_2'
        sql_cmd1 = f'''
                        drop foreign table if exists {self.t_name_1};
                        drop foreign table if exists {self.t_name_2};
                        create foreign table {self.t_name_1}
                        (id int,name varchar(10));
                        create foreign table {self.t_name_2}
                        (id int,name varchar(10));
                        insert into {self.t_name_1} values(1,'测试1'),
                        (2,'b'),(3,'测试2'),(4,'d'),(5,'e');
                        insert into {self.t_name_2} values(2,'e'),
                        (3,'测试3'),(4,'f'),(5,'l'),(6,'f');
                    '''
        msg4 = self.primary_sh.execut_db_sql(sql_cmd1)
        self.logger.info(msg4)
        sql_cmd2 = f'''
                        select t1.name name from {self.t_name_1} t1 
                        left join {self.t_name_2} t2 on t1.id=t2.id;
                    '''
        msg5 = self.primary_sh.execut_db_sql(sql_cmd2)
        self.logger.info(msg5)
        self.assertIn('测试1', msg5)
        sql_cmd3 = f'''
                        select t1.name name from {self.t_name_1} t1
                        right join {self.t_name_2} t2 on t1.id=t2.id;
                    '''
        msg6 = self.primary_sh.execut_db_sql(sql_cmd3)
        self.logger.info(msg6)
        self.assertIn('测试2', msg6)
        sql_cmd4 = f'''
                        select t1.name name from {self.t_name_1} t1 
                        join {self.t_name_2} t2 on t1.id=t2.id;
                    '''
        msg7 = self.primary_sh.execut_db_sql(sql_cmd4)
        self.logger.info(msg7)
        self.assertIn('测试2', msg7)
        sql_cmd5 = f'''
                        drop foreign table {self.t_name_1};
                        drop foreign table {self.t_name_2};
                    '''
        msg8 = self.primary_sh.execut_db_sql(sql_cmd5)
        self.logger.info(msg8)
        self.assertIn(self.constant.DROP_FOREIGN_SUCCESS_MSG, msg8)
        
    def tearDown(self):
        self.logger.info('-----this is tearDown-----')
        self.logger.info('-----恢复配置，enable_incremental_checkpoint=on-----')
        show_msg = self.common.show_param(self.config)
        if 'on' not in show_msg:
            msg9 = self.primary_sh.execute_gsguc('set',
                                             self.constant.GSGUC_SUCCESS_MSG,
                                             f'{self.config}=on')
            self.logger.info(msg9)
            msg10 = self.primary_sh.restart_db_cluster()
            self.logger.info(msg10)
            msg11 = self.primary_sh.get_db_cluster_status()
            self.assertTrue("Normal" or "Degrade" in msg11)
        show_msg = self.common.show_param(self.config)
        self.assertTrue('on' in show_msg)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
